/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetlif13.model.tictactoe;

import projetlif13.model.Game2D;
import projetlif13.model.Move;
import projetlif13.model.Player;

/**
 *
 * @author theo
 */
public class TicGame extends Game2D {

    public TicGame(Player _p1, Player _p2) {
        super(_p1, _p2);
        this.gameBoard = new TicBoard();
        if (_p1 instanceof IAPlayerTic) {
            ((IAPlayerTic) _p1).start();
        }
        if (_p2 instanceof IAPlayerTic) {
            ((IAPlayerTic) _p2).start();
        }
    }

    @Override
    public boolean gameEnded() {

        //test des lignes
        for (int y = 0; y < gameBoard.getHeight(); y++) {
            int i = gameBoard.getPIDByXY(0, y);
            boolean samePlayer = true;
            for (int x = 0; x < gameBoard.getWidth(); x++) {
                if (gameBoard.getPIDByXY(x, y) != i || gameBoard.getPIDByXY(x, y) == -1) {
                    samePlayer = false;
                }
            }
            if (samePlayer) {
                return true;
            }
        }
        //test des colones
        for (int x = 0; x < gameBoard.getWidth(); x++) {
            int i = gameBoard.getPIDByXY(x, 0);
            boolean samePlayer = true;
            for (int y = 0; y < gameBoard.getHeight(); y++) {
                if (gameBoard.getPIDByXY(x, y) != i || gameBoard.getPIDByXY(x, y) == -1) {
                    samePlayer = false;
                }
            }
            if (samePlayer) {
                return true;
            }
        }
        //Pour les diagonales :
        if (gameBoard.getPIDByXY(1, 1) != -1) {
            if (gameBoard.getPIDByXY(0, 0) == gameBoard.getPIDByXY(1, 1) && gameBoard.getPIDByXY(0, 0) == gameBoard.getPIDByXY(2, 2)) {
                return true;
            }
            if (gameBoard.getPIDByXY(2, 0) == gameBoard.getPIDByXY(1, 1) && gameBoard.getPIDByXY(2, 0) == gameBoard.getPIDByXY(0, 2)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean moveAvailable(Move _m) {
        if (gameBoard.getPIDByXY(_m.x, _m.y) == -1) {
            return true;
        } else {
            return false;
        }
    }
}
